package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * 港口商船
 */
public class ShipGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {
        //是否停留
        boolean stop = pos == endPos;
        if(!stop){
            return;
        }

        GridHandleResult gridHandleResult = new GridHandleResult();
        //判断是否解锁
        CrossXingYouShanShuiTeamData teamData = result.getActivity().getTeamData(result.getUserData().getTeamId());
        //如果是漂泊状态
        if(result.getUserData().isDrift()){
            teamData = result.getActivity().getTeamData(result.getUserData().getMapTeamId());
        }

        XingYouShanShuiProto.ShanShuiEventInfo.Builder builder = XingYouShanShuiProto.ShanShuiEventInfo.newBuilder();
        Property property = new Property();

        Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, true);

        property.addProperty(fixedReward);
        builder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));

        builder.setBaseReward(PropertyHelper.parsePropertyToString(property));
        //累计消耗骰子条件满足
//        if(result.getActivity().getTeamTotalUseEnergy(teamData.getTeamId()) >= gridInfo.getParam().get(0)){
        property.addProperty(PropertyHelper.parseStringToProperty(result.getConfig().getSHANSHUI_GOLD_COIN_ID() + "=" + gridInfo.getParam().get(1)));
//        }

        result.getActivity().calRewardBuff(result.getUserData(), property, false, 0);

        builder.setGridId(gridId);
        builder.setPosition(pos);
        builder.setAward(PropertyHelper.parsePropertyToString(property));
        gridHandleResult.setBuilder(builder);

        ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, false, result.getUserData().isDrift());
        gridHandleResult.setGridParam(gridParam);
        result.getGridHandleResultList().add(gridHandleResult);

        CrossXingYouShanShuiUserData userData = result.getUserData();
        //是否有捕快
        result.getActivity().tryCatch(pos, userData, result, gridHandleResult);
    }

}
